package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //查询用户列表
    @GetMapping
    public Result list(UserDto userDto) {
        log.info("用户列表查询 ",userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    //新增用户
    @Log
    @PostMapping
    public Result add(@RequestBody User user) {
        log.info("新增用户 ",user);
        userService.add(user);
        return Result.success();
    }
    //批量删除(可以删除一个 或者 批量删除)
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids) {
        log.info("批量删除用户:{} ",ids);
        userService.deleteUsers(ids);
        return Result.success();
    }
    //根据Id查询用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询用户信息  " + id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    //修改用户信息
    @Log
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户信息  " + user);
        userService.updateUsers(user);
        return Result.success();
    }
    //查询所有用户信息
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有用户信息");
        List<User> list = userService.list();
        return Result.success(list);
    }
    //根据角色查询用户
    //请求路径：/users/role/{roleLabel}
    //请求方式：GET
    //接口描述：该接口用于根据角色标识查询用户列表
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户列表  " + roleLabel);
        List<User> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }
    //根据部门查询用户信息
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        log.info("根据部门查询用户列表  " + deptId);
        List<User> list = userService.listByDept(deptId);
        return Result.success(list);
    }
}
